package com.um.login.action;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.cs.framework.common.bo.Message;
import com.cs.framework.ui.web.UI;
import com.cs.framework.web.annotation.Action;
import com.cs.framework.web.annotation.Mapping;
import com.cs.framework.web.handler.Web;
import com.um.info.dto.base.UmUserDto;
import com.um.login.service.LoginService;

/**
 * UM登陆
 * 
 * @author hedongzhou
 * 
 */
@Action("login")
public class LoginAction {

	private Log logger = LogFactory.getLog(getClass());

	@Resource
	private LoginService loginService;

	/**
	 * 进入登陆页面
	 */
	@Mapping
	public void loginPage() {
		UI.returnPage("um_login");
	}

	/**
	 * 进入主页
	 */
	@Mapping
	public void homePage() {
		UI.returnPage("um_home");
	}

	/**
	 * 登陆
	 */
	@Mapping
	public Object login() {
		UmUserDto umUserDto = Web.bind(UmUserDto.class);

		try {
			umUserDto = loginService.checkLogin(umUserDto);

			Web.setUser(umUserDto);

			return new Message("Y", "登陆成功");
		} catch (Exception e) {
			logger.error(e.getMessage(), e);

			return new Message("N", e.getMessage());
		}
	}

	/**
	 * 退出
	 */
	@Mapping
	public void logout() {
		Web.setUser(null);
		Web.getSession().setMaxInactiveInterval(1);

		loginPage();
	}

}
